package com.lavida.project.wage.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lavida.common.utils.poi.ExcelUtil;
import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.aspectj.lang.annotation.Log;
import com.lavida.framework.aspectj.lang.enums.BusinessType;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.framework.web.domain.AjaxResult;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.service.IUserService;
import com.lavida.project.wage.domain.NewWageAccount;
import com.lavida.project.wage.domain.ProvideRecord;
import com.lavida.project.wage.mapper.NewWageAccountMapper;
import com.lavida.project.wage.service.ProvideRecordService;

/**
 * 用户信息
 * 
 */
@Controller
@RequestMapping("/wage/providerecord")
public class ProvideRecordController extends BaseController
{
    private String prefix = "wage/providerecord";
    
    @Autowired
    private ProvideRecordService provideRecordService;
    
    @Autowired
	private NewWageAccountMapper newWageAccountMapper;

    @Autowired
	private IUserService userService;
    @RequiresPermissions("payroll:providerecord:view")
    @GetMapping()
    public String provideRecord(ModelMap mmap)
    {
    	User user  = ShiroUtils.getSysUser();
		User user1 = userService.selectUserById(user.getUserId());
		mmap.put("user", user1);
        return prefix + "/providerecord";
    }
    

    @RequiresPermissions("payroll:providerecord:list")
    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> list(ProvideRecord provideRecord)
    {
    	Map<String,Object> result = new HashMap<>();
    	List<ProvideRecord> selectProvideRecordList = provideRecordService.selectProvideRecordList(provideRecord);
    	result.put("provideRecordList", selectProvideRecordList);
    	return result;
    }
    /**
     * 获取当年最后一条记录的月份
     * @param provideRecord
     * @return
     */
    @RequiresPermissions("payroll:providerecord:list")
    @PostMapping("/getLast")
    @ResponseBody
    public AjaxResult getLast(ProvideRecord provideRecord)
    {
        try
        {
            return AjaxResult.success("操作成功",provideRecordService.getLastProvideRecord(provideRecord));
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return error(e.getMessage());
        }
    }
    
    

    @Log(title = "发放记录", businessType = BusinessType.EXPORT)
    @RequiresPermissions("payroll:newwage:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(NewWageAccount newWageAccount)
    {
    	List<NewWageAccount> list = newWageAccountMapper.selectNewWageAccountList(newWageAccount);
        ExcelUtil<NewWageAccount> util = new ExcelUtil<NewWageAccount>(NewWageAccount.class);
        return util.exportExcel(list, "核算新工资");
    }
    
    @RequiresPermissions("payroll:providerecord:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(ProvideRecord provideRecord)
    {
    	System.out.println(provideRecord);
    	provideRecord.setCreateBy(ShiroUtils.getSysUser().getLoginName());
    	provideRecord.setCreateTime(new Date());
        try
        {
            return toAjax(provideRecordService.addProvideRecord(provideRecord));
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return error(e.getMessage());
        }
    }
    @RequiresPermissions("payroll:providerecord:afresh")
    @PostMapping("/reaccount")
    @ResponseBody
    public AjaxResult reAccount(ProvideRecord provideRecord)
    {
    	provideRecord.setCreateBy(ShiroUtils.getSysUser().getLoginName());
    	provideRecord.setCreateTime(new Date());
        try
        {
            return toAjax(provideRecordService.editProvideRecord(provideRecord));
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return error(e.getMessage());
        }
    }
    
    
    
    
    @RequiresPermissions("wage:providerecord:remove")
    @Log(title = "发放记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestBody ProvideRecord provideRecord)
    {
        try
        {
            return toAjax(provideRecordService.deleteProvideRecord(provideRecord));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }   
}